package com.kaka.bus.engine.user.login;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.LoginBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/9/28.
 */

public class LoginPresenter extends RxPresenter<LoginContract.View> implements LoginContract.Presenter{

    @Inject
    public LoginPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void isExist(String phone) {
        /**
         * 检查手机格式
         */
        mView.showDialog("验证手机号码...");
        addSubscribe(mDataManager
                .getApi()
                .phoneIsExist(phone)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> data) {
                        mView.phoneState(data);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void login(String phone, String pwd, String tokenKey,String systemVersion,String phoneType) {
        mView.showDialog("登录中..");
        addSubscribe(mDataManager
                .getApi()
                .login(phone,pwd,tokenKey, Constant.PLATFORM,systemVersion,phoneType)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<LoginBean>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<LoginBean> bean) {
                        mView.loginSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void sendSms(String phone, String tokenKey, String event, String device) {
        mView.showDialog("发送验证码..");

        addSubscribe(mDataManager.getApi()
                .getCode(phone,tokenKey,event,device)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> wrapper) {
                        mView.sendSuccess(wrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public void loginOnSms(String phone, String authCode, String tokenKey, String device,String systemVersion,String phoneType) {
        mView.showDialog("验证码登录中..");
        addSubscribe(mDataManager
                .getApi()
                .loginByAuthCode(phone,authCode,tokenKey, device,systemVersion,phoneType)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<LoginBean>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<LoginBean> bean) {
                        mView.loginSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        mView.dissDialog();
                    }
                }));
    }
}
